<template>
    <el-form :model="roleForm" ref="form" :rules="rules" label-width="80px" :inline="false" size="normal">
        <el-form-item label="名称" prop="rolename">
            <el-input v-model="roleForm.rolename"></el-input>
        </el-form-item>
        <el-form-item label="描述">
            <el-input v-model="roleForm.description"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">确定</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup lang="ts">
import { RequestRoleAdd, RequestRoleEdit } from '@/api/index'
import { ref, type Ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import type { IRole } from '@/utils/types'


const roleForm: Ref<IRole> = ref({ r_id: 0, rolename: '', description: '' })
const rules = ref({
    rolename: [{ required: true, message: '请输入名称', trigger: 'blur' }],
})
const props = defineProps(['role', 'formType'])
const emit = defineEmits(['close'])

onMounted(() => {
    if (props.formType == 'ADD') {
        roleForm.value = { r_id: 0, rolename: '', description: '' }
    } else {
        roleForm.value = { ...props.role }
    }
})

// 操作角色
const onSubmit = async () => {
    if (props.formType == 'ADD') {
        const data = await RequestRoleAdd(roleForm.value)
        const { resultCode } = data
        if (resultCode === 1) {
            ElMessage.success('添加角色成功!')
            emit('close')
        }
    } else {
        const data = await RequestRoleEdit(roleForm.value)
        const { resultCode, resultInfo } = data
        if (resultCode === 1) {
            ElMessage.success('编辑角色成功!')
            emit('close')
        }
    }
}
</script>

<style scoped>

</style>
